package com.mcy.view.locateview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
 * 定位采集工具控件
 * 通过移动画布上十字工具实现点对准
 * 通过定位十字中心对准点的屏幕坐标
 * @author Administrator
 *
 */
public class LocateView extends View{
	
	//画布坐标起点控件左上角为原点（与父控件无关）
	private int mCurrentCenterX = 500;
	private int mCurrentCenterY = 500;
	
	private float mTouchX;
	private float mTouchY;
	private float mMoveX = 0;
	private float mMoveY = 0;
	
	private Paint mPaint;
	private Paint mBasePaint;

	public LocateView(Context context) {
		super(context);
        init();
	}
	
	public LocateView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public LocateView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init(){
		mPaint = new Paint();
		mPaint.setColor(Color.BLUE);
		mPaint.setStyle(Style.STROKE);
		mPaint.setStrokeWidth(2);
		mBasePaint = new Paint();
		mBasePaint.setColor(Color.BLACK);
		mBasePaint.setStyle(Style.STROKE);
		mBasePaint.setStrokeWidth(2);
		setFocusableInTouchMode(true);
	}

	@Override
	public boolean performClick() {
		// TODO Auto-generated method stub
		return super.performClick();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mPaint.setColor(Color.RED);
			mTouchX = event.getX();//控件坐标系，而非屏幕坐标系
			mTouchY = event.getY();//屏幕坐标系 event.getRawX()
			break;
		case MotionEvent.ACTION_UP:
			mPaint.setColor(Color.BLUE);
			mMoveX = 0;
			mMoveY = 0;
			performClick();
			break;
		case MotionEvent.ACTION_MOVE:
			mMoveX = event.getX()-mTouchX;
			mMoveY = event.getY()-mTouchY;
			mTouchX = event.getX();
			mTouchY = event.getY();
			mCurrentCenterX += mMoveX;
			mCurrentCenterY += mMoveY;
			break;
		default:
			break;
		}
		invalidate();
		/*
		 * view onTouch()返回true后续监听不会转发到父控件,且不会自动触发onclick事件
		 * 返回false则会转发到父控件，如果不是foucused则中断接收监听 ，会自动触发onclick事件
		 */
		return true;
//		return super.onTouchEvent(event);
	}

	/*
     * Step 1: 在measure()函数中调用，完成后返回view宽，高在layout中的推荐值
     * 在此处设置宽高可以覆盖测量值
     */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		//直接设置控件尺寸
		//setMeasuredDimension(150, 150);
	}

    /*
     * Step 2: 在layout()函数中调用，完成view在layout中的实际布局
     * 在这里可以更改布局位置
     */
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
	}
	
    /*
     * Step 3: 在draw()函数中调用，自定义view绘制
     */
	@Override
	protected void onDraw(Canvas canvas) {
		//画布坐标系与控件位置无关，与屏幕坐标相同
		super.onDraw(canvas);
		/*不使用资源图片，自定义绘制*/
//		mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.target_tm_2);
//		Rect rect = new Rect(
//				(int)mCurrentCenterX-75, 
//				(int)mCurrentCenterY-75, 
//				(int)mCurrentCenterX+75, 
//				(int)mCurrentCenterY+75);
//		canvas.drawBitmap(mBitmap,null,rect, mPaint);

		// 画与Y轴平行坐标
		canvas.drawLine(mCurrentCenterX,0,mCurrentCenterX,mCurrentCenterY-20, mBasePaint);
		canvas.drawLine(mCurrentCenterX,mCurrentCenterY+20,mCurrentCenterX,getHeight(), mBasePaint);
		// 画与X轴平行坐标
		canvas.drawLine(0,mCurrentCenterY,mCurrentCenterX-20,mCurrentCenterY, mBasePaint);
		canvas.drawLine(mCurrentCenterX+20,mCurrentCenterY,getWidth(),mCurrentCenterY, mBasePaint);
//		//边线
//		canvas.drawLine(mCurrentCenterX-25, mCurrentCenterY-100, 
//				mCurrentCenterX+25, mCurrentCenterY-100, mPaint);
//		canvas.drawLine(mCurrentCenterX-25, mCurrentCenterY+100, 
//				mCurrentCenterX+25, mCurrentCenterY+100, mPaint);
//		canvas.drawLine(mCurrentCenterX-100, mCurrentCenterY-25, 
//				mCurrentCenterX-100, mCurrentCenterY+25, mPaint);
//		canvas.drawLine(mCurrentCenterX+100, mCurrentCenterY-25, 
//				mCurrentCenterX+100, mCurrentCenterY+25, mPaint);
		
		//中心方形定位框
		canvas.drawRect(mCurrentCenterX-20, mCurrentCenterY-20,
				mCurrentCenterX+20, mCurrentCenterY+20, mBasePaint);
		
		//中心圆形定位框
//		canvas.drawCircle(mCurrentCenterX, mCurrentCenterY,40, mPaint);
		// 画中心点
//		canvas.drawRect(mCurrentCenterX-2, mCurrentCenterY-2
//				mCurrentCenterX+2, mCurrentCenterY+2, mBasePaint);
		canvas.drawLine(mCurrentCenterX, mCurrentCenterY-5, mCurrentCenterX, mCurrentCenterY+5, mPaint);
		canvas.drawLine(mCurrentCenterX-5, mCurrentCenterY, mCurrentCenterX+5, mCurrentCenterY, mPaint);
	}
	public int getCenterX(){
		return mCurrentCenterX;
	}
	public int getCenterY(){
		return mCurrentCenterY;
	}
	public void moveTo(int x,int y){
		mCurrentCenterX = x;
		mCurrentCenterY = y;
		invalidate();
	}
}
